﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using BLL;
using Model;
using System.IO;
using System.Text.RegularExpressions;
using NCRE学生考试端V1._0.判分操作类;

namespace NCRE学生考试端V1._0
{
    public partial class FrmLogin : Form
    {
        #region 定义代码
        /// <summary>
        /// 定义一个ExamConfigBLL类
        /// </summary>
        ExamConfigBll examconfigbll = new ExamConfigBll();
        /// <summary>
        /// 定义一个WordQuestionEntityBLL类
        /// </summary>
        WordQuestionEntityBll wordquestionbll = new WordQuestionEntityBll();
        /// <summary>
        /// 定义一个WinQuestionEntityBLL类
        /// </summary>
        WinQuestionEntityBll winquestionbll = new WinQuestionEntityBll();
        /// <summary>
        /// 定义一个ExcelQuestionEntityBLL类
        /// </summary>
        ExcelEntityBll excelQuestionBll = new ExcelEntityBll();
        /// <summary>
        /// 定义一个IEBLL类
        /// </summary>
        IeQuestionEntityBll iequestionbll = new IeQuestionEntityBll();
        /// <summary>
        /// 定义一个StudentBindPaperTypeBLL类
        /// </summary>
        StudentBindPaperTypeBLL studentBindPaperType = new StudentBindPaperTypeBLL();
        /// <summary>
        /// 定义一个PPTBLL类
        /// </summary>
        PptQuestionEntityBLL pptQuestionBll = new PptQuestionEntityBLL();
        /// <summary>
        /// 定义一个学生类
        /// </summary>
        StudentInfoBll studentinfoBll = new StudentInfoBll();
        /// <summary>
        /// 定义一个日志类
        /// </summary>
        LogBll logBll = new LogBll();
        /// <summary>
        /// 登陆学号
        /// </summary>
        public static string studentID
        {
            get;
            set;
        }
        #endregion

        /// <summary>
        /// 登陆单例模式
        /// </summary>
        private static FrmLogin instanceLogin;
        /// <summary>
        /// 登陆单例模式
        /// </summary>
        /// <returns></returns>
        public static FrmLogin GetInstance()
        {
            if (instanceLogin == null)
            {
                instanceLogin = new FrmLogin();

            }
            return instanceLogin;
        }

        public FrmLogin()
        {
            InitializeComponent();
        }
        #region 窗体加载
        private void FrmLogin_Load(object sender, EventArgs e)
        {
            studentID = txtStudentID.Text.Trim();
            //固定窗体大小-李芬-2015年12月27日
            MaximumSize = new Size(690, 500);
            MinimumSize = new Size(690, 500);
            palNotice.Visible = false;
            palEntity.Visible = false;
            palLogin.Visible = true;
            timer1.Stop();
            //打开窗体默认光标显示位置-刘檬-2018年12月7日
            this.txtStudentID.Focus();
            this.txtStudentID.Select();

            #region 根据练习模式和考试模式判断label1的显示内容--白爱民--2018年12月20日-21点23分
            DataTable dt = new LogBll().QuerySetUpBll();
            if (dt.Rows[0]["is_upload"].ToString() == "0")   //根据是否有提示框来判断，因为is_exam没有地方修改过，一直为1
            {
                label1.Text = "请在左侧输入准考证号并点击【登录】按钮确认个人信息";
            }
            else
            {
                label1.Text = "请在左侧输入验证码并点击【登录】按钮确认个人信息";
            }
            #endregion

        }
        #endregion

        #region 确定按钮
        private void btnLogin_Click(object sender, EventArgs e)
        {
            //查询考试模式设置信息-谷海涛
            DataTable dtSetUp = new LogBll().QuerySetUpBll();

            //根据学生code取出学生信息
            StudentInfoEntity studentinfo = new StudentInfoEntity();
            StudentInfoEntityBLL studentinfobll = new StudentInfoEntityBLL();
            StudentBindTypeEntityBll studentbindtypeentitybll = new StudentBindTypeEntityBll();
            StudentBindPaperTypeEntity studentbindinfo = new StudentBindPaperTypeEntity();

            #region 判断输入的是否是学号或者验证码-苑仁杰-2018-12-21--修改人：谷海涛-2018年12月28日18:51:37


            if (dtSetUp.Rows[0]["is_upload"].ToString() == "0")//考试模式下，不能用验证码
            {
                if (txtStudentID.TextLength == 4)//如果是4位的验证码
                {
                    MessageBox.Show("请使用学号登录！");
                    txtStudentID.Text = "";
                    txtStudentID.Focus();
                    return;
                }
                if (txtStudentID.TextLength != 4 && txtStudentID.TextLength != 11)
                {
                    MessageBox.Show("请输入正确的学号！");
                    txtStudentID.Focus();
                    txtStudentID.Clear();
                    return;
                }
                //走学号
                else if (txtStudentID.TextLength == 11)//如果是11位的学号
                {
                    studentinfo.studentID = txtStudentID.Text.Trim();//白爱民增加
                                                                     //学号查询--白爱民-2018年12月20日 20点47分
                    DataTable table1 = studentinfobll.SelectStudentInfoByID(studentinfo);//根据学号查询表

                    #region 添加文本框清空功能和判断学生是否存在-苑仁杰-2018年12月18日10点11分
                    if (table1.Rows.Count == 0)
                    {
                        MessageBox.Show("学号错误，请重新输入！");

                        txtStudentID.Text = "";
                        txtStudentID.Focus();

                        return;
                    }
                    #endregion
                    studentinfo.studentID = table1.Rows[0]["studentID"].ToString();
                    studentID = table1.Rows[0]["studentID"].ToString();
                    studentbindinfo.StudentID = table1.Rows[0]["studentID"].ToString();
                }
            }
            if (dtSetUp.Rows[0]["is_upload"].ToString() == "1")//练习模式下，验证码或者学号都可以用
            {
                if (txtStudentID.TextLength == 4)//如果是4位的验证码
                {
                    //走验证码
                    studentinfo.code = txtStudentID.Text.Trim();//苑仁杰增加
                                                                //验证码查询-苑仁杰-2018-12-20
                    DataTable table = studentinfobll.SelectStudentInfoBycode(studentinfo);//根据验证码查询表
                    #region 添加文本框清空功能和判断学生是否存在-苑仁杰-2018年12月18日10点11分
                    if (table.Rows.Count == 0)
                    {
                        MessageBox.Show("验证码错误，请输入正确的验证码！");

                        txtStudentID.Text = "";
                        txtStudentID.Focus();

                        return;
                    }
                    #endregion
                    //把验证码绑定的学号赋给实体-苑仁杰
                    studentinfo.studentID = table.Rows[0]["studentID"].ToString();
                    studentID = table.Rows[0]["studentID"].ToString();
                    studentbindinfo.StudentID = table.Rows[0]["studentID"].ToString();
                }


                //走学号
                else if (txtStudentID.TextLength == 11)//如果是11位的学号
                {
                    studentinfo.studentID = txtStudentID.Text.Trim();//白爱民增加
                                                                     //学号查询--白爱民-2018年12月20日 20点47分
                    DataTable table1 = studentinfobll.SelectStudentInfoByID(studentinfo);//根据学号查询表

                    #region 添加文本框清空功能和判断学生是否存在-苑仁杰-2018年12月18日10点11分
                    if (table1.Rows.Count == 0)
                    {
                        MessageBox.Show("学号错误，请重新输入！");

                        txtStudentID.Text = "";
                        txtStudentID.Focus();

                        return;
                    }
                    #endregion
                    studentinfo.studentID = table1.Rows[0]["studentID"].ToString();
                    studentID = table1.Rows[0]["studentID"].ToString();
                    studentbindinfo.StudentID = table1.Rows[0]["studentID"].ToString();
                }
                if (txtStudentID.TextLength != 4 && txtStudentID.TextLength != 11)
                {
                    MessageBox.Show("请输入正确的验证码或者学号！");
                    txtStudentID.Focus();
                    txtStudentID.Clear();
                    return;
                }

            }
            #endregion

            #region 判断输入的是否是学号或者验证码-苑仁杰-2018-12-21注释不用

            //if (txtStudentID.TextLength != 4 && txtStudentID.TextLength != 11)
            //{
            //    MessageBox.Show("您的输入错误或为空，请重新输入");
            //    txtStudentID.Focus();
            //    txtStudentID.Clear();
            //}
            //else
            //{
            //    if (txtStudentID.TextLength == 4)
            //    {
            //        //走验证码
            //        studentinfo.code = txtStudentID.Text.Trim();//苑仁杰增加
            //        //验证码查询-苑仁杰-2018-12-20
            //        DataTable table = studentinfobll.SelectStudentInfoBycode(studentinfo);//根据验证码查询表
            //        #region 添加文本框清空功能和判断学生是否存在-苑仁杰-2018年12月18日10点11分
            //        if (table.Rows.Count == 0)
            //        {
            //            MessageBox.Show("验证码错误，请重新输入！");

            //            txtStudentID.Text = "";
            //            txtStudentID.Focus();

            //            return;
            //        }
            //        #endregion
            //        //把验证码绑定的学号赋给实体-苑仁杰
            //        studentinfo.studentID = table.Rows[0]["studentID"].ToString();
            //        studentID = table.Rows[0]["studentID"].ToString();
            //        studentbindinfo.StudentID = table.Rows[0]["studentID"].ToString();



            //    }
            //    else
            //    {
            //        //走学号
            //        studentinfo.studentID = txtStudentID.Text.Trim();//白爱民增加
            //        //学号查询--白爱民-2018年12月20日 20点47分
            //        DataTable table1 = studentinfobll.SelectStudentInfoByID(studentinfo);//根据学号查询表
            //        #region 添加文本框清空功能和判断学生是否存在-苑仁杰-2018年12月18日10点11分
            //        if (table1.Rows.Count == 0)
            //        {
            //            MessageBox.Show("学号错误，请重新输入！");

            //            txtStudentID.Text = "";
            //            txtStudentID.Focus();

            //            return;
            //        }
            //        #endregion
            //        studentinfo.studentID = table1.Rows[0]["studentID"].ToString();
            //        studentID = table1.Rows[0]["studentID"].ToString();
            //        studentbindinfo.StudentID = table1.Rows[0]["studentID"].ToString();

            //    }
            #endregion

            #region 学生登录加上判断，当天只能登录一次——苑仁杰——2018年12月22日15:22:12 注释了

            //studentbindinfo.TimeTamp = DateTime.Now.ToLongDateString();
            //DataTable dttime = studentbindtypeentitybll.SelectStudentByStudentIDTime(studentbindinfo);//查询是否登录过
            //                                                                                          //判断里面当天是否有值
            //if (dttime.Rows.Count > 0)
            //{
            //    return;
            //}
            ////没有网情况下把成绩传给公众号
            #endregion

            #region 根据查询结果判断是否重复登录或者二次登录-谷海涛-2018年12月25日11:44:10
            //判断学生是否已经登录过此系统-韩梦甜-2015-12-14

            //studentbindinfo.StudentID = txtStudentID.Text.Trim();

            DataTable dtIsUser =  studentbindtypeentitybll.SelectStudentByIsUse(studentbindinfo);

                DataTable dt = studentinfobll.SelectStudentInfoByID(studentinfo);

                

                //if (dtIsUser.Rows.Count > 0)
                //{
                    //取出dt的值,1为正在考试，2为上传文件异常，0为未在线

                    //if (dtIsUser.Rows[0]["IsUse"].ToString() == "1")
                    //{
                    //    MessageBox.Show("该学号正在上机考试！");
                    //    txtStudentID.Focus();
                    //    return;
                    //}
                    //else if (dtIsUser.Rows[0]["IsUse"].ToString() == "2")
                    //{
                    //    MessageBox.Show("该学号上传文件出现问题,请联系管理员!");
                    //    return;
                    //}
                    //else if (dtIsUser.Rows[0]["IsUse"].ToString() == "0")
                    //{
                    //    MessageBox.Show("已经是二次登录");
                    //    //this.Close();
                    //    //frmSecondLogin frmSecond = new frmSecondLogin();
                    //    //frmSecond.ShowDialog(); 

                    //}
                //}

                #endregion

                #region 显示学生信息Panel，隐藏登录Panel
                palEntity.Visible = true;
                btnNext.Focus();
                palLogin.Visible = false;
                //考场信息
                lblexamPlaceID.Text = dt.Rows[0]["examPlaceID"].ToString();
                //准考证号
                lblstudentID.Text = dt.Rows[0]["studentID"].ToString();
                //学生姓名性别
                lblstuName.Text = dt.Rows[0]["studentName"].ToString();
                #endregion
                //全局变量存入学生姓名，学号
                LoginEntity.studentName = dt.Rows[0]["studentName"].ToString();
                LoginEntity.studentID = dt.Rows[0]["studentID"].ToString();

                #region 登录成功之后清空该验证码-苑仁杰-2018年12月18日10点11分              
                studentinfo.studentID = studentID;
                int flag = studentinfobll.UpdateStudentInfoByID(studentinfo);
                #endregion
            
            #region 注释代码
            //#region 添加文本框判空功能-孙邵聪-2018年12月13日13点13分
            //            if(txtStudentID.Text=="")
            //            {
            //                MessageBox.Show("请输入准考证号");
            //                txtStudentID.Focus();
            //            }
            //#endregion
            //#region 添加文本框判空功能-苑仁杰-2018年12月18日10点13分
            //if (txtStudentID.Text == "")
            //{
            //    MessageBox.Show("请输入验证码");
            //    txtStudentID.Focus();
            //}
            //#endregion

            //if (dt.Rows.Count == 0)
            //{
            //    MessageBox.Show("学生不存在！");
            //#region 添加文本框清空功能-孙邵聪-2018年12月13日13点11分
            //txtStudentID.Text = "";
            //txtStudentID.Focus();
            //#endregion

            //return;
            //}               
            //studentinfo.studentID = table.Rows[0]["studentID"].ToString();              
            //建议文本框为默认值 "请在此处提建议，若无建议则点击下一步!"
            //txtQuestion.Text = "请在此处提建议，若无建议则点击下一步!";
            //btnsub和txtQuestion控件可见
            //txtQuestion.Visible = true;
            //btnSub.Visible = true;
            #endregion

        }
        #endregion

        #region 配置题型
        /// <summary>
        /// 配置题型
        /// </summary>
        private void BindPaperType()
        {

            //studentID = txtStudentID.Text.Trim();
            //验证学生是否可以考试
            StudentInfoBll studentBll = new StudentInfoBll();
             StudentInfoEntity enStudent = studentBll.GetStudentById(studentID);
            if (enStudent == null)
            {
                MessageBox.Show("学生不存在！");
            }
            Hide();
            //时间戳
            string timetamp = "";
            //判分完了之后插入的
            int d = 0;

            StudentBindTypeEntityBll studentbindtypeentitybll = new StudentBindTypeEntityBll();

            StudentBindPaperTypeEntity studentbindinfo = new StudentBindPaperTypeEntity() { StudentID = studentID };
            DataTable dt = studentbindtypeentitybll.SelectStudentByIsUse(studentbindinfo);
            bool IsBindPaperType = true;      //需要配题
            if (dt.Rows.Count != 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    if (dr["IsUse"].ToString() == "1")
                    {
                        IsBindPaperType = false;    //不需要配题

                        //TODO 更新登陆的IP
                        StudentInfoEntity studentInfo = studentinfoBll.GetStudentById(studentID);
                        StudentBindPaperTypeEntity studentBindPaperTypeEntity = new StudentBindPaperTypeEntity();
                        studentBindPaperTypeEntity.StudentID = studentInfo.studentID;
                        studentBindPaperTypeEntity.IP = MyInfo.GetInternalIP();
                        studentBindPaperTypeEntity.TimeTamp = timetamp;

                        studentBindPaperType.updateLoginIP(studentBindPaperTypeEntity);

                        #region 复制考生文件夹从C盘到D盘-韩梦甜-2016-1-4
                        string Dpath = string.Format("D:\\{0}\\计算机一级考生文件\\{1}卷\\", studentID, MyInfo.MyPaperType());
                        string Cpath = string.Format("C:\\计算机一级考生文件\\{0}卷\\", MyInfo.MyPaperType());
                        //如果不存在，则复制
                        if (!Directory.Exists(Dpath))
                        {
                            if (!CopyFolder.Copy(Cpath, Dpath))
                            {
                                MessageBox.Show("从C盘到D盘拷贝失败！");
                            }
                            else
                            {
                                //如果存在，判断文件夹是否为空
                                string[] gg = System.IO.Directory.GetDirectories(Dpath);
                                int num = gg.Length;

                                //如果为空，则复制文件夹
                                if (num != 0)
                                {
                                    if (File.GetAttributes(string.Format("D:\\" + studentID + "\\计算机一级考生文件")).ToString().IndexOf("ReadOnly") != -1)
                                    {
                                        File.SetAttributes(string.Format("D:\\" + studentID + "\\计算机一级考生文件"), FileAttributes.Normal);
                                        File.Delete(Dpath);
                                    }
                                    if (!CopyFolder.Copy(Cpath, Dpath))
                                    {
                                        MessageBox.Show("从C盘到D盘拷贝失败！");
                                    }
                                }
                            }
                        }
                        #endregion
                    }
                }
            }

            if (IsBindPaperType)
            {
                //进行配题   ，打上时间戳
                //————————————开始配题————————————————————————————————————————————————————————————————————————————


                string collegeId;
                if (studentID != "")
                {
                    StudentInfoEntity studentInfo = studentinfoBll.GetStudentById(studentID);

                    //给该学生分配题

                    #region ++++++++++++++++++++Word++++++++++++++++++++

                    collegeId = studentInfo.CollegeID;
                    //3、WordQuestionEntity中查找出所有的信息。将不同的ABCd等试卷的信息存放在一个list中
                    DataTable worddt = wordquestionbll.WordPaperType();

                    List<List<WordQuestionEntity>> wordquestionlist = new List<List<WordQuestionEntity>>();
                    string Papertype = "";
                    if (worddt.Rows.Count != 0)
                    {
                        for (int i = 0; i < worddt.Rows.Count; i++)
                        {
                            Papertype = worddt.Rows[i]["PaperType"].ToString();
                            wordquestionlist.Add(wordquestionbll.WordPaperTypeGroupByPaperType(Papertype));
                        }

                        //4、给学生分配数据的PaperType,添加WordRecord表             

                        if (!examconfigbll.IsTableExist("WordQuestionRecordEntity_" + collegeId))
                        {
                            examconfigbll.CreateDataTableCopySelectRecord("WordQuestionRecordEntity_" + collegeId, "WordQuestionRecordEntity");
                        }

                        List<StudentBindPaperTypeEntity> studentBindPaperTypeEntityList = new List<StudentBindPaperTypeEntity>();      //这里在StudentBindPaperTypeEntity中insert
                        List<WordQuestionRecordEntity> wordrecoredStudentlist = new List<WordQuestionRecordEntity>();

                        string strStudentID = studentInfo.studentID;
                        WordQuestionRecordEntity wordStudent = new WordQuestionRecordEntity();
                        wordStudent.StudentID = strStudentID;

                        int wordi = wordquestionlist.Count;

                        Random ra = new Random();
                        d = ra.Next(0, wordi);

                        //判断此时是考试模式还是联系模式
                        StudentBindPaperTypeEntity studentBindPaperTypeEntity = new StudentBindPaperTypeEntity();
                        DataTable entable = new DataTable();
                        entable = logBll.QuerySetUpBll();
                        if (entable.Rows[0]["is_showScore"].ToString() == "0" && entable.Rows[0]["is_upload"].ToString() == "0" && entable.Rows[0]["is_lockScreen"].ToString() == "0")
                        {
                            studentBindPaperTypeEntity.ExamModel = "考试模式";
                        }
                        else
                        {
                            studentBindPaperTypeEntity.ExamModel = "练习模式";
                        }

                        studentBindPaperTypeEntity.StudentID = studentInfo.studentID;
                        studentBindPaperTypeEntity.CollegeID = studentInfo.CollegeID;
                        studentBindPaperTypeEntity.PaperType = wordquestionlist[d][0].PaperType;
                        studentBindPaperTypeEntity.IP = MyInfo.GetInternalIP();
                        studentBindPaperTypeEntity.IsUse = 1;
                        //studentBindPaperTypeEntity.TimeTamp = timetamp


                        studentBindPaperTypeEntityList.Add(studentBindPaperTypeEntity);

                        studentBindPaperType.InsertRecordList(studentBindPaperTypeEntityList);

                        timetamp = MyInfo.TimeTamp();


                        for (int m = 0; m < wordquestionlist[d].Count; m++)
                        {
                            WordQuestionRecordEntity wordrecoredStudent = new WordQuestionRecordEntity();
                            wordrecoredStudent.StudentID = strStudentID;
                            wordrecoredStudent.QuestionID = wordquestionlist[d][m].QuestionID;
                            wordrecoredStudent.QuestionContent = wordquestionlist[d][m].QuestionContent;
                            wordrecoredStudent.PaperType = wordquestionlist[d][m].PaperType;
                            wordrecoredStudent.RightAnswer = wordquestionlist[d][m].RightAnswer;
                            wordrecoredStudent.TimeStamp = timetamp;
                            wordrecoredStudentlist.Add(wordrecoredStudent);
                        }
                        wordquestionbll.InsertWordRecordList(wordrecoredStudentlist, studentInfo);
                    }
                    #endregion

                    #region ++++++++++++++++++++选择题++++++++++++++++++++
                    //如果选中全部，则清空所有的选择题答题记录
                    //examConfigBll.ClearSelectQuestionRecordByCollegeID(allCollege)
                    //examconfigbll.ClearSelectQuestionRecordByLstStudent(new List<StudentInfoEntity>() { studentInfo })
                    examconfigbll.RandGenerateRecord(new List<StudentInfoEntity>() { studentInfo }, timetamp);
                    #endregion

                    #region ++++++++++++++++++++Win++++++++++++++++++++
                    //WinQuestionEntity中查找出所有的信息。将不同的ABCd等试卷的信息存放在一个list中
                    DataTable winddt = winquestionbll.WinPaperType();

                    List<List<WinQuestionEntity>> winquestionWhich = new List<List<WinQuestionEntity>>();

                    string WinPapertype = "";

                    if (winddt.Rows.Count != 0)
                    {

                        for (int i = 0; i < winddt.Rows.Count; i++)
                        {
                            WinPapertype = winddt.Rows[i]["paperType"].ToString();
                            winquestionWhich.Add(winquestionbll.WinPaperTypeGroupByPaperType(WinPapertype));
                        }
                        if (examconfigbll.IsTableExist("WinQuestionRecordEntity_" + collegeId) == false)
                        {
                            examconfigbll.CreateDataTableCopySelectRecord("WinQuestionRecordEntity_" + collegeId, "WinQuestionRecordEntity");
                        }
                        //进行分配

                        //做循环
                        List<WinQuestionRecordEntity> winrecoredStudentlist = new List<WinQuestionRecordEntity>();

                        string StrStudentID = studentInfo.studentID;
                        WinQuestionRecordEntity winStudent = new WinQuestionRecordEntity();
                        winStudent.studentID = StrStudentID;

                        //int wini = winquestionWhich.Count
                        //Random ra = new Random()
                        //int d = ra.Next(0, wini - 1)
                        for (int m = 0; m < winquestionWhich[d].Count; m++)
                        {
                            WinQuestionRecordEntity winrecoredStudent = new WinQuestionRecordEntity();
                            winrecoredStudent.studentID = StrStudentID;
                            winrecoredStudent.questionID = winquestionWhich[d][m].questionID;
                            winrecoredStudent.questionContent = winquestionWhich[d][m].questionContent;
                            winrecoredStudent.paperType = winquestionWhich[d][m].paperType;
                            winrecoredStudent.correctAnswer = winquestionWhich[d][m].correctAnswer;
                            winrecoredStudent.timeStamp = timetamp;
                            winrecoredStudentlist.Add(winrecoredStudent);
                        }
                        winquestionbll.InsertWinRecordList(winrecoredStudentlist, studentInfo);
                    }
                    #endregion

                    #region ++++++++++++++++++++Excel++++++++++++++++++++

                    //3、ExcelQuestionEntity中查找出所有的信息。将不同的ABCd等试卷的信息存放在一个list中
                    DataTable exceldt = excelQuestionBll.ExcelPaperType();

                    List<List<ExcelQuestionEntity>> excelquestionlist = new List<List<ExcelQuestionEntity>>();
                    string excelPapertype = "";

                    if (exceldt.Rows.Count != 0)
                    {
                        for (int i = 0; i < exceldt.Rows.Count; i++)
                        {
                            excelPapertype = exceldt.Rows[i]["PaperType"].ToString();
                            excelquestionlist.Add(excelQuestionBll.ExcelPaperTypeGroupByPaperType(excelPapertype));
                        }

                        //4、给学生分配数据的PaperType,添加WordRecord表
                        if (examconfigbll.IsTableExist("ExcelQuestionRecordEntity_" + collegeId) == false)
                        {
                            examconfigbll.CreateDataTableCopySelectRecord("ExcelQuestionRecordEntity_" + collegeId, "ExcelQuestionRecordEntity");
                        }

                        //进行分配

                        List<ExcelQuestionRecordEntity> excelrecoredStudentlist = new List<ExcelQuestionRecordEntity>();

                        string strStudentIDExcel = studentInfo.studentID;
                        ExcelQuestionRecordEntity excelStudent = new ExcelQuestionRecordEntity();
                        excelStudent.StudentID = strStudentIDExcel;

                        for (int m = 0; m < excelquestionlist[d].Count; m++)
                        {
                            ExcelQuestionRecordEntity excelrecoredStudent = new ExcelQuestionRecordEntity();
                            excelrecoredStudent.StudentID = strStudentIDExcel;
                            excelrecoredStudent.QuestionID = excelquestionlist[d][m].QuestionID;
                            excelrecoredStudent.QuestionContent = excelquestionlist[d][m].QuestionContent;
                            excelrecoredStudent.PaperType = excelquestionlist[d][m].PaperType;
                            excelrecoredStudent.CorrectAnswer = excelquestionlist[d][m].CorrectAnswer;
                            excelrecoredStudent.TimeStamp = timetamp;
                            excelrecoredStudentlist.Add(excelrecoredStudent);
                        }
                        excelQuestionBll.InsertExcelRecordList(excelrecoredStudentlist, studentInfo);
                    }
                    #endregion

                    #region ++++++++++++++++++++IE++++++++++++++++++++

                    //IEQuestionEntity中查找出所有的信息。将不同的ABCd等试卷的信息存放在一个list中
                    DataTable Ieddt = iequestionbll.WinPaperType();

                    List<List<IEQuestionEntity>> iequestionWhich = new List<List<IEQuestionEntity>>();
                    string iePapertype = "";

                    if (Ieddt.Rows.Count != 0)
                    {

                        for (int i = 0; i < Ieddt.Rows.Count; i++)
                        {
                            iePapertype = Ieddt.Rows[i]["paperType"].ToString();
                            iequestionWhich.Add(iequestionbll.IEPaperTypeGroupByPaperType(iePapertype));
                        }

                        if (examconfigbll.IsTableExist("IEQuestionRecordEntity_" + collegeId) == false)
                        {
                            examconfigbll.CreateDataTableCopySelectRecord("IEQuestionRecordEntity_" + collegeId, "IEQuestionRecordEntity");
                        }

                        //进行分配

                        //做循环
                        List<IEQuestionRecordEntity> ierecoredStudentlist = new List<IEQuestionRecordEntity>();

                        string strStudentIDIE = studentInfo.studentID;
                        IEQuestionRecordEntity IEStudent = new IEQuestionRecordEntity();
                        IEStudent.studentID = strStudentIDIE;


                        //int iei = iequestionWhich.Count
                        //Random ra = new Random()
                        //int d = ra.Next(0, iei - 1)

                        for (int m = 0; m < iequestionWhich[d].Count; m++)
                        {
                            IEQuestionRecordEntity ierecoredStudent = new IEQuestionRecordEntity();
                            ierecoredStudent.studentID = strStudentIDIE;
                            ierecoredStudent.questionID = iequestionWhich[d][m].questionID;
                            ierecoredStudent.questionContent = iequestionWhich[d][m].questionContent;
                            ierecoredStudent.paperType = iequestionWhich[d][m].paperType;
                            ierecoredStudent.correctAnswer = iequestionWhich[d][m].correctAnswer;
                            ierecoredStudent.timeStamp = timetamp;
                            ierecoredStudentlist.Add(ierecoredStudent);
                        }
                        iequestionbll.InsertIERecordList(ierecoredStudentlist, studentInfo);


                    }
                    #endregion

                    #region ++++++++++++++++++++Ppt++++++++++++++++++++

                    //3、PptQuestionEntity中查找出所有的信息。将不同的ABCd等试卷的信息存放在一个list中
                    DataTable pptdt = pptQuestionBll.PptPaperType();

                    List<List<PptQuestionEntity>> pptQuestionlist = new List<List<PptQuestionEntity>>();
                    string PapertypePPT = "";
                    if (pptdt.Rows.Count != 0)
                    {
                        for (int i = 0; i < pptdt.Rows.Count; i++)
                        {
                            PapertypePPT = pptdt.Rows[i]["PaperType"].ToString();
                            pptQuestionlist.Add(pptQuestionBll.PptPaperTypeGroupByPaperType(PapertypePPT));
                        }

                        //4、给学生分配数据的PaperType,添加WordRecord表



                        if (examconfigbll.IsTableExist("PptQuestionRecordEntity_" + collegeId) == false)
                        {
                            examconfigbll.CreateDataTableCopySelectRecord("PptQuestionRecordEntity_" + collegeId, "PptQuestionRecordEntity");
                        }

                        //进行分配

                        List<PptQuestionRecordEntity> pptrecoredStudentlist = new List<PptQuestionRecordEntity>();

                        string strStudentIDPpt = studentInfo.studentID;
                        PptQuestionRecordEntity pptStudent = new PptQuestionRecordEntity();
                        pptStudent.StudentID = strStudentIDPpt;

                        //int ppti = pptQuestionlist.Count
                        //Random ra = new Random()
                        //int d = ra.Next(0, ppti - 1)

                        for (int m = 0; m < pptQuestionlist[d].Count; m++)
                        {
                            PptQuestionRecordEntity pptrecoredStudent = new PptQuestionRecordEntity();
                            pptrecoredStudent.StudentID = strStudentIDPpt;
                            pptrecoredStudent.QuestionID = pptQuestionlist[d][m].QuestionID;
                            pptrecoredStudent.QuestionContent = pptQuestionlist[d][m].QuestionContent;
                            pptrecoredStudent.PaperType = pptQuestionlist[d][m].PaperType;
                            pptrecoredStudent.RightAnswer = pptQuestionlist[d][m].RightAnswer;
                            pptrecoredStudent.TimeStamp = timetamp;
                            pptrecoredStudentlist.Add(pptrecoredStudent);
                        }
                        pptQuestionBll.InsertPptRecordList(pptrecoredStudentlist, studentInfo);

                    }
                    #endregion

                    #region 将考生文件从C盘拷贝到D盘-韩梦甜-2015-12-25
                    string path = string.Format("D:\\" + studentID + "\\计算机一级考生文件\\" + MyInfo.MyPaperType() + "卷\\");
                    //如果文件不存在，则复制
                    if (!Directory.Exists(string.Format("D:\\" + studentID + "\\计算机一级考生文件")))
                    {
                        //开始复制
                        if (!CopyFolder.Copy(string.Format("C:\\计算机一级考生文件\\" + MyInfo.MyPaperType() + "卷\\"), path))
                        {
                            MessageBox.Show("从C盘到D盘拷贝失败！");
                        }
                    }
                    else
                    {
                        FrmJudge frmjudge = new FrmJudge();
                        frmjudge.Kill();
                        DirectoryInfo di = new DirectoryInfo(string.Format("D:\\" + studentID + "\\计算机一级考生文件\\"));


                        //若文件为只读状态，将其更改为非只读状态
                        if (System.IO.File.GetAttributes("D:\\" + studentID + "\\计算机一级考生文件\\").ToString().IndexOf("ReadOnly") != -1)
                        {
                            File.SetAttributes("D:\\" + studentID + "\\计算机一级考生文件\\", FileAttributes.Normal);


                        }

                        // 判断如果是考试模式，则不删除D盘的考生答题记录--刁琳童--2019年1月3日
                        DataTable setup = new LogBll().QuerySetUpBll();
                        if (setup.Rows[0]["is_exam"].ToString() != "0")
                            DeleteFileByDirectory(di);
                        
                        //di.Delete(true);
                        //开始复制
                        if (!CopyFolder.Copy(string.Format("C:\\计算机一级考生文件\\" + MyInfo.MyPaperType() + "卷\\"), path))
                        {
                            MessageBox.Show("从C盘到D盘拷贝失败！");
                        }
                    }
                    #endregion

                }
                else
                {
                    MessageBox.Show("请填写学生的考号！");
                }
                //_______________________配题结束--------------------------------------------------------------------------------------------------------------------------------------------------------
            }

            FrmMain frmmain = new FrmMain();
            frmmain.ShowDialog();


        }
        #endregion
        /// <summary>

        /// 用来遍历删除目录下的文件以及该文件夹

        /// </summary>

        #region // 删除此目录下的文件--刁琳童--2019年1月3日
        public void DeleteFileByDirectory(DirectoryInfo info)
        {
            foreach (DirectoryInfo newInfo in info.GetDirectories())
            {
                DeleteFileByDirectory(newInfo);
            }
            foreach (FileInfo newInfo in info.GetFiles())
            {
                newInfo.Attributes = newInfo.Attributes & ~(FileAttributes.Archive | FileAttributes.ReadOnly | FileAttributes.Hidden);
                newInfo.Delete();
            }
            info.Attributes = info.Attributes & ~(FileAttributes.Archive | FileAttributes.ReadOnly | FileAttributes.Hidden);
            info.Delete();
        }
        #endregion

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Hide();
            Application.Exit();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            int i = int.Parse(Regex.Replace(label4.Text, @"[^\d.\d]", ""));
            if (i == 0)
            {
                timer1.Stop();
                BindPaperType();
            }
            i--;
            label4.Text = i + "秒";
        }

        private void btnRelogin_Click(object sender, EventArgs e)
        {
            palLogin.Visible = true;
            palEntity.Visible = false;
            txtStudentID.Focus();
            //重新登录返回登录界面并选中输入内容
            txtStudentID.SelectAll();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            palEntity.Visible = false;
            palNotice.Visible = true;
            timer1.Start();
            //查询考生须知等待时间
            DataTable dt = new LogBll().QuerySetUpBll();
            if (dt.Rows.Count != 0)
            {
                label3.Text = string.Format("倒计时{0}秒自动登录:", dt.Rows[0]["noticeTime"].ToString());
                label4.Text = string.Format("{0}秒", dt.Rows[0]["noticeTime"].ToString());
            }
        }





        #region 补充：提交建议的模块-孙邵聪-2018年12月2日11:29:33 注释代码
        ////提交建议    将此模块移动到悬浮框位置处
        //private void btnSub_Click(object sender, EventArgs e)
        //{
        //    StudentInfoEntity studentinfo = new StudentInfoEntity();
        //    studentinfo.studentID = txtStudentID.Text.Trim();
        //    StudentInfoEntityBLL studentinfobll = new StudentInfoEntityBLL();
        //    DataTable dt = studentinfobll.SelectStudentInfoByID(studentinfo);


        //    QuestionEntity question = new QuestionEntity();
        //    question.Question = txtQuestion.Text.ToString();
        //    question.Studentid = dt.Rows[0]["studentID"].ToString();
        //    TestQuestionBLL TQuestion = new TestQuestionBLL();


        //    //如果文本框内的内容不为空，也不是默认值，那么就进行提交操作
        //    if (txtQuestion.Text != "" && txtQuestion.Text != "请在此处提建议，若无建议则点击下一步!")
        //    {
        //        int result = TQuestion.InsertQuestion(question);
        //        if (result > 0)
        //        {
        //            MessageBox.Show("谢谢您宝贵的建议！");
        //            txtQuestion.Text = "请在此处提建议，若无建议则点击下一步!";
        //            btnSub.Visible = false;
        //            txtQuestion.Visible = false;
        //            btnNext.Focus();

        //        }
        //    }
        //    //文本框为空操作或等于默认值时为空操作

        //}

        ////txtQuestion文本框显示默认值：请在此处提建议，若无建议则点击下一步
        //private void txtQuestion_MouseLeave(object sender, EventArgs e)
        //{
        //    //判断建议文本框是否为空，若不为空则鼠标移动出控件范围后，内容还是输入的内容
        //    if (txtQuestion.Text != "")
        //    {
        //        string str = txtQuestion.Text;
        //        txtQuestion.Text = str;
        //    }
        //    //如果文本框没有输入值，则鼠标移出控件范围后，内容变为默认内容
        //    else
        //    {
        //        txtQuestion.Text = "请在此处提建议，若无建议则点击下一步!";
        //        btnNext.Focus();
        //    }
        //}


        ////鼠标点击后，txtQuestion文本框内容为空
        //private void txtQuestion_MouseClick(object sender, MouseEventArgs e)
        //{

        //    //如果txtQuestion中的内容为默认值，则再次点击之后清空内容
        //    if (txtQuestion.Text == "请在此处提建议，若无建议则点击下一步!")
        //    {
        //        txtQuestion.Text = "";
        //    }
        //    //如果不是默认值则是已经输入的内容
        //    else
        //    {
        //        string str = txtQuestion.Text;
        //        txtQuestion.Text = str;
        //    }

        //}


        ////限制文本框：不能输入非法字符
        //private void txtQuestion_KeyPress(object sender, KeyPressEventArgs e)
        //{

        //    //只能输入数字0-9，字母A-Z，a-z，不能输入汉字
        //    //if (e.KeyChar <= '9' && e.KeyChar >= '0' || e.KeyChar <= 'Z' && e.KeyChar >= 'A' || e.KeyChar <= 'z' && e.KeyChar >= 'a')
        //    //{
        //    //    e.Handled = false;
        //    //}
        //    //else
        //    //{
        //    //    e.Handled = true;
        //    //}


        //    //if (e.KeyChar >= 33 && e.KeyChar <= 46 || e.KeyChar >= 58 && e.KeyChar <= 64 )
        //    //{
        //    //    e.Handled = true;
        //    //}
        //    //else
        //    //{
        //    //    e.Handled = true;
        //    //}


        //    check(e);



        //}
        //public void check(KeyPressEventArgs e)
        //{
        //    char[] charstr = { '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '（', '）', '<', '>', '?', ':', '"', '{', '}', '~', '-', '[', ']', '|' };// 非法字符数组（有几个就写几个用单引号引上‘’用，点隔开）
        //    for (int i = 0; i < charstr.Length; i++)
        //    {
        //        if (e.KeyChar == charstr[i])
        //            e.Handled = true;
        //    }
        //}



        #endregion

        private void palEntity_Paint(object sender, PaintEventArgs e)
        {

        }

        #region 限制输入内容只能为数字和删除键-刘檬-2018年12月7日
        private void txtStudentID_KeyPress_1(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && (Keys)(e.KeyChar) != Keys.Back)
            {
                e.Handled = true;
            }
        }
        #endregion
    }
}
